package io.renren.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Preconditions;
import io.renren.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.entity.CommentEntity;
import io.renren.entity.UserEntity;
import io.renren.service.CommentService;
import io.renren.service.UserService;
import io.renren.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-01-20 12:55:41
 */
@RestController
@RequestMapping("api/comment")
public class CommentController {


    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;


    /**
     * 列表
     */
    @SysLog("查询评论列表")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = commentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @SysLog("查询评论")
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        CommentEntity comment = commentService.selectById(id);

        return R.ok().put("comment", comment);
    }

    /**
     * 保存
     */
    @SysLog("保存评论")
    @RequestMapping("/save")
    public R save(@RequestBody CommentEntity comment) {
        boolean isSuccess = commentService.insert(comment);
        if (isSuccess) {
            return R.ok().put("commentId", comment.getId());
        } else {

            return R.error("操作失败");
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CommentEntity comment) {
        ValidatorUtils.validateEntity(comment);
        commentService.updateAllColumnById(comment);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除评论")
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        commentService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据项目查询评论
     */
    @SysLog("根据项目查评论")
    @RequestMapping("/infoByCID")
    public R infoByCID(@RequestParam Map<String, Object> params) {

        Preconditions.checkArgument(null != params.get("campaignId"), "活动ID不能为空");

        int campaignId = Integer.parseInt(params.get("campaignId").toString());

        Page<CommentEntity> commentEntityPage = commentService.selectPage(new Query<CommentEntity>(params).getPage(), new EntityWrapper<CommentEntity>().eq("campaign_id", campaignId).eq("reply_id", 0));

        List<CommentEntity> records = commentEntityPage.getRecords();

        for (CommentEntity item : records) {
            // 如果回复的内容不为空，则返回评论的那条内容
//                CommentEntity replyEntity = commentService.selectById(item.getReplyId());
            item.setUserName(this.getUserName(item.getUserId()));

            List<CommentEntity> replyEntity = commentService.selectList(new EntityWrapper<CommentEntity>().eq("reply_id", item.getId()));

            for(CommentEntity replyItem: replyEntity) {

                replyItem.setUserName(this.getUserName(replyItem.getUserId()));
            }

            item.setReplyList(replyEntity);
        }

        return R.ok().put("comment", commentEntityPage);
    }

    private String getUserName(String userId){
        UserEntity  userEntity = userService.selectById(userId);
        if(null == userEntity) {
            return "匿名用户";
        }else{
            return userEntity.getUserName();
        }
    }


}
